<template>
    <div class="backgrounimg">
        <div class="divwhite">
            <div class="headtext">欢迎登录志愿者培训系统后台</div>
            <div class="bodytext">
                <el-tabs v-model="activeName">
                    <el-tab-pane label="管理员登入" name="first">
                        <!-- 表单 -->
                        <el-form label-position="top" :model="Sign" :rules="rules" ref="Sign" label-width="100px"
                            class="demo-ruleForm">
                            <el-form-item label="手机号" prop="mobile">
                                <el-input v-model="Sign.mobile"></el-input>
                            </el-form-item>
                            <el-form-item label="密码" prop="password">
                                <el-input v-model="Sign.password"></el-input>
                            </el-form-item>
                            <el-button type="primary" @click="Signin">登录</el-button>
                        </el-form>
                    </el-tab-pane>
                </el-tabs>
            </div>
        </div>
    </div>
</template>

<script>
import { Login } from '../api/Login.js'
export default {
    data() {
        return {
            // tab 栏切换
            activeName: 'first',
            // 表单验证规则
            rules: {
                mobile: [
                    { required: true, message: '用户名不能为空', trigger: 'blur' },
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' },
                ],
            },
            // 登录要传的数据
            Sign: {
                mobile: '13853108236',
                password: '108236'
            }
        };
    },
    methods: {

        // 点击登录按钮
        Signin() {
            // 判断输入框是否为空
            this.$refs.Sign.validate((v) => {
                if (!v) return
                var fromData = JSON.parse(JSON.stringify(this.Sign))
                fromData.password = fromData.password + ''
                // 请求接口
                this.login()
            })

        },
        // 请求区😚😚😚😚😚
        // 请求 登录
        async login() {
            const { data: res } = await Login(this.Sign)
            console.log(res);
            // 判断是否成功
            if (res.code !== 1) return this.$message.error(`登录失败,原因：${res.message}`)
            // // 在缓存中设置token
            sessionStorage.setItem('token', `Bearer ${res.token}`)
            // sessionStorage.setItem('token', 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6Ingxamx4bWo1biIsInJvbGUiOiJhZG1pbi1hZG1pbiJ9.BTJDyIga9pJrFWb2p_Rumm6jwEaA8IpFhnnaRInDnLY')
            this.$message.success('登录成功')
            this.$router.push('/')
        }
    }
}

</script>

<style scoped>
.backgrounimg {
    position: relative;
    overflow: hidden;
    height: 100%;
    width: 100%;
    background: url("../assets/img/login.jpg") no-repeat;
}

.divwhite {
    position: absolute;
    top: 50%;
    right: 12%;
    transform: translateY(-50%);
    width: 300px;
    border-radius: 6px;
    box-shadow: 0px 0px 7px 0px rgba(0, 0, 0, 0.7);
    background-color: white;
}

.headtext {
    padding: 14px 16px;
    border-bottom: 1px solid #000;
}

.el-form-item__label {
    line-height: 24px;
}

.el-input__inner {
    height: 30px;
}

.bodytext {
    padding: 16px;
    padding-bottom: 40px;
}
</style>
